package cn.com.backstage.controller;




import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_resource;
import cn.com.api.service.ApiU_resourceService;
import cn.com.backstage.controller.base.U_resourceBaseController;
import cn.com.util.*;

/**
 * 资源控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class U_resourceController  extends U_resourceBaseController {

	private static Logger logger = Logger.getLogger(U_resourceController.class);
	
	@Autowired
	private ApiU_resourceService apiU_resourceService;
	
	/**
	 * 音乐列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "resource-music-list")
	public ModelAndView u_resource_music_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_resource_music_list.jsp");
		Map<String, Object> param = new HashMap<String, Object>();
		try {			
			String type = request.getParameter("type");
			String res_name = request.getParameter("res_name");		
			param.put("type", type);
			if(res_name!= null && !("").equals(res_name)){			
				param.put("res_name", res_name);
			}
			List<ApiU_resource> u_resources = apiU_resourceService.findU_resourcesByCondition(param);
			mv.addObject("u_resources", u_resources);
		} catch (Exception e) {
			logger.error("=============[U_resourceController[u_resource_music_list[获取列表失败]]]==========",e);
		}		
		return mv;
	}
	
	/**
	 * 图片列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "resource-image-list")
	public ModelAndView u_resource_image_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_resource_image_list.jsp");
		Map<String, Object> param = new HashMap<String, Object>();			
		try {			
			String type = request.getParameter("type");
			String res_name = request.getParameter("res_name");
			param.put("type", type);
			if(res_name!= null && !("").equals(res_name)){			
				param.put("res_name", res_name);
			}
			List<ApiU_resource> u_resources = apiU_resourceService.findU_resourcesByCondition(param);
			mv.addObject("u_resources", u_resources);
		} catch (Exception e) {
			logger.error("=============[U_resourceController[u_resource_image_list[获取列表失败]]]==========",e);
		}	
		return mv;
	}
	
	
	
	
	/**
	 * 资源编辑
	 */
	@RequestMapping(value = "resource-add")
	public void u_resource_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		try {		
			
		//	String resource_id = request.getParameter("resource_id");
			String resource_id = request.getParameter("id");
			
			String res_name = request.getParameter("res_name");
			String res_url = request.getParameter("res_url");
			String status = request.getParameter("status");
			String res_image = request.getParameter("res_image");		
			String type = request.getParameter("type");
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String now_time = sd.format(new Date());	
			//add
			if(resource_id==null || ( resource_id.trim()).equals("")){
	
				ApiU_resource u_resource = new ApiU_resource();
				u_resource.setRes_name(res_name);
				u_resource.setRes_url(res_url);
				u_resource.setCreate_time(now_time);
				u_resource.setStatus(status);
				u_resource.setRes_image(res_image);
				u_resource.setType(type);			
			apiU_resourceService.insertU_resource_info(u_resource);
			}
			//edit
			else{
				ApiU_resource u_resource = apiU_resourceService.findU_resourceById(Integer.valueOf(resource_id));
				u_resource.setResource_id(Integer.parseInt(resource_id));
				u_resource.setRes_name(res_name);
				u_resource.setRes_url(res_url);
				u_resource.setCreate_time(now_time);
				u_resource.setStatus(status);
				u_resource.setRes_image(res_image);
				u_resource.setType(type);		
				apiU_resourceService.updateU_resource(u_resource);
			//	apiU_resourceService.updateU_resource_info(u_resource);
		
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		} catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION+e.fillInStackTrace());
			map.put(Utils.MESSAGE, "添加失败！");
			e.fillInStackTrace();
			logger.error("=============[U_resourceController[u_resource_add[资源增加失败]]]==========",e);
		}
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "resource-add-pre")
	public ModelAndView u_resource_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_resource_add.jsp");
		String type = request.getParameter("type");
		 
		
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			
			
			ApiU_resource u_resource = apiU_resourceService.findU_resourceById(Integer.valueOf(id));
			
			mv.addObject("u_resource",u_resource);
				
		}
		mv.addObject("type",type);
		return mv;
	}
	
	
	public static void main(String[] args) {
		String ss = "[{RES_URL=111.jpg, RES_NAME=广告1, RES_IMAGE=olcvpwfyn.bkt.clouddn.com/bb352bd36b89a95d1018a47f787dbcea.jpg, RESOURCE_ID=22221}, {RES_URL=111, RES_NAME=科比, RES_IMAGE=olcvpwfyn.bkt.clouddn.com/634883270a51259bedb9a3a9a76363f2.jpg, RESOURCE_ID=4}]";
		String[]  array = ss.split("\\{");
		List<String> list = new ArrayList<String>();
		for (String string : array) {
			string = string.replace("}", "").replace("]", "").replace("[", "");
			list.add(string);
		}
		String xx = "634883270a51259bedb9a3a9a76363f2.jpg";
		String result = "";
		for (String object : list) {
			if(object.contains(xx)){
				result = object;
			}
		}
		String resultImage = "";
		String[] resultSplt = result.split(",");
		for (String string : resultSplt) {
			if(string.contains("RES_IMAGE")){
				resultImage = string;
			}
		}
		System.out.println(resultImage);
	}
}



